<template>
  <BasicModal v-bind="$attrs" @register="userRegister" :title="getTitle" width="1000px" okText="确认" @ok="submitAudit">
    <BasicTable @register="registerTable" :loading="loading" :pagination="{ pageSize: 10 }"
      :rowSelection="{ type: 'checkbox' }"> </BasicTable>
  </BasicModal>
</template>

<script lang="ts" setup>
import { BasicModal, useModalInner } from '/@/components/Modal';
import { ref, reactive } from 'vue';
import { useListPage } from '/@/hooks/system/useListPage';
import { itemsList } from '../SysUser.api';
import { itemColumn } from '../SysUser.data';
import { BasicTable } from '/@/components/Table';

const loading = ref(false);
const getTitle = ref('选择实验项目');

const queryParam = reactive<any>({});
const formData = reactive<Record<string, any>>({
  itemId: '',
});
let defaultSelect = ref([])

function resetA() {
  formData.itemId = '';
}
const emits = defineEmits<{
  (event: "getSelectResult", devList: Array<any>): void;
}>();

const [userRegister, { setModalProps, closeModal }] = useModalInner(async (data) => {
  setModalProps({
    minHeight: 220,
  });
  resetA();
  reload();
  defaultSelect.value = [];
  if(data.tableData.length>0){
     data.tableData.forEach(element => {
      defaultSelect.value.push(element.itemId)
    })
  }
  setSelectedRowKeys(defaultSelect.value);
  // rowSelection.selectedRowKeys.value = keys;
  loading.value = true;
});

// 注册table数据
const { tableContext } = useListPage({
  tableProps: {
    api: itemsList,
    columns: itemColumn,
    canResize: false,
    useSearchForm: false,
    showIndexColumn: true,
    showTableSetting: false,
    showActionColumn: false,
    beforeFetch: (params) => {
      return Object.assign(params, queryParam);
    },
  },
});

const [registerTable, { getSelectRows,setSelectedRowKeys, clearSelectedRowKeys, reload }] = tableContext;

async function submitAudit() {
  const selectedRows = getSelectRows();
  if (selectedRows.length > 0) {
    emits('getSelectResult', selectedRows);
    clearSelectedRowKeys();
  }
  closeModal();
}
defineExpose({
  reload
})
</script>
